package com.geekcattle.controller.goods;

import com.geekcattle.core.utils.ShiroUtil;
import com.geekcattle.service.goods.GoodsCarService;
import com.geekcattle.util.MapUtil;
import com.geekcattle.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * @program: geek-framework
 * @description:
 * @author: LQ
 * @create: 2019-04-21 11:20
 */
@RestController//@Controller和@ResponseBody的组合
@RequestMapping("/goodsCar")
public class GoodsCarController {
    @Autowired
    private GoodsCarService goodsCarService;

    /**
     * 获取当前用户的所有购物车信息
     * 包含商品信息
     * @param map
     * @return
     */
    @RequestMapping(value= "/getCar" , method={RequestMethod. GET,RequestMethod.POST})
    public Map getCar(@RequestParam Map map){
        map.put("userId",this.getId());
        return MapUtil.resultMap(goodsCarService.getCar(map));
    }

    /**
     * 删除购物车中的商品
     * @param map
     * @return
     */
    @RequestMapping(value= "/deleteCar" , method={RequestMethod. GET,RequestMethod.POST,RequestMethod.DELETE})
    public Map deleteCar(@RequestParam Map map){
        int result = goodsCarService.deleteCar(map);
        return MapUtil.infoMap(result,result>0?"删除成功":"删除失败");
    }

    /**
     * 修改购物车中的商品
     * @param map
     * @return
     */
    @RequestMapping(value= "/updateCar" , method={RequestMethod. GET,RequestMethod.POST})
    public Map updateCar(@RequestParam Map map){
        int result = goodsCarService.updateCar(map);
        return MapUtil.infoMap(result,result>0?"修改成功":"修改失败");
    }

    /**
     * 添加购物车
     * @param map
     * @return
     */
    @RequestMapping(value= "/addCar" , method={RequestMethod. GET,RequestMethod.POST,RequestMethod.PUT})
    public Map addCar(@RequestParam Map map){
        map.put("id",UuidUtil.getUUID());
        map.put("userId",this.getId());
        map.put("create_time",new Date());
        try {
            goodsCarService.addCar(map);
            return MapUtil.infoMap("添加购物车成功");
        }catch (Exception e){
            e.printStackTrace();
            return  MapUtil.infoMap("添加购物车失败");
        }
    }

    /**
     * 获取当前登录用户的id
     * @return
     */
    private String getId(){
        String uid = ShiroUtil.getUserInfo().getUid();
        return uid;
    }
}